package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.TbRoleDao;
import com.woniuxy.daos.TbUserDao;
import com.woniuxy.entitys.TbLog;
import com.woniuxy.entitys.TbMalfunction;
import com.woniuxy.entitys.TbRole;
import com.woniuxy.entitys.TbRoleExample;
import com.woniuxy.services.FaultService;
import com.woniuxy.services.SystemLogService;
import com.woniuxy.utils.ResultVO;
import com.woniuxy.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 操作日志
 */
@RestController
@RequestMapping("/log")
public class SystemLogController {
    @Autowired
    private SystemLogService systemLogService;
    @Autowired
    private TbRoleDao tbRoleDao;

    /**
     * 查询所有日志记录(分页)
     * roleSelectValue参数根据角色查询：(null对应全部日志；角色id对应该角色的日志)
     * @param map
     * @return
     */
    @PostMapping("/getAllLog")
    public ResultVO getAllLog(@RequestBody Map<String, Integer> map){
        List<TbRole> roles = tbRoleDao.selectByExample(new TbRoleExample());
        List<TbLog> allLog = systemLogService.getAllLog(map.get("pageNum"), map.get("pageSize"), map.get("roleSelectValue"));
        if (allLog.size() == 0) {
            return ResultVO.failure("没有数据");
        }
        PageInfo<TbMalfunction> pageInfo = new PageInfo(allLog);
        Map<String, Object> map1 = new HashMap();
        map1.put("roles", roles);
        map1.put("pageInfo", pageInfo);
        return ResultVO.success("查询成功", map1);
    }

    /**
     * 模糊查询日志记录(分页)
     * @param map
     * @return
     */
    @PostMapping("/getAllLogByFuzz")
    public ResultVO getAllLogByFuzz(@RequestBody Map<String, String> map) {
        List<TbLog> allLogByFuzz = systemLogService.getAllLogByFuzz(Integer.parseInt(map.get("pageNum")), Integer.parseInt(map.get("pageSize")), map.get("searchValue"));
        if (allLogByFuzz.size() == 0) {
            return ResultVO.failure("没有数据");
        }
        PageInfo<TbMalfunction> pageInfo = new PageInfo(allLogByFuzz);
        return ResultVO.success("查询成功", pageInfo);
    }

}
